home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_08 / 9n08094a < prev    next >
Text File  |  1991-06-26  |  2KB  |  71 lines

  1.  
  2. /* Encode a scan line and write it to a PCX file (the line is   */
  3. /* assumed to contain the color plane scan lines in sequence,   */
  4. /* with padding for an even number of bytes and trailing white  */
  5. /* data for each line as appropriate)                           */
  6.  
  7. void pcx_write_line
  8. (
  9.   unsigned char *linep, /* Scan line buffer pointer             */
  10.   int length,           /* Scan line buffer length (in bytes)   */
  11.   FILE *fp              /* PCX file pointer                     */
  12. )
  13. {
  14.   int curr_data;        /* Current data byte                    */
  15.   int prev_data;        /* Previous data byte                   */
  16.   int data_count;       /* Data repeat count                    */
  17.   int line_count;       /* Scan line byte count                 */
  18.  
  19.   prev_data = *linep++; /* Initialize the previous data byte    */
  20.   data_count = 1;
  21.   line_count = 1;
  22.  
  23.   while (line_count < length)   /* Encode scan line             */
  24.   {
  25.     curr_data = *linep++;       /* Get the current data byte    */
  26.     line_count++;               /* Increment line byte count    */
  27.  
  28.     if (curr_data == prev_data) /* Repeating data bytes ?       */
  29.     {
  30.       data_count++;             /* Increment data repeat count  */
  31.  
  32.       if (data_count == 0x3f)   /* Max allowable repeat count ? */
  33.       {
  34.         pcx_encode(prev_data, data_count, fp);  /* Encode data  */
  35.         data_count = 0;
  36.       }
  37.     }
  38.     else    /* End of repeating data bytes                      */
  39.     {
  40.       if (data_count > 0)
  41.         pcx_encode(prev_data, data_count, fp);  /* Encode data  */
  42.  
  43.       prev_data = curr_data;    /* Current data byte now prev   */
  44.       data_count = 1;
  45.     }
  46.   }
  47.  
  48.   if (data_count > 0)           /* Any remaining data ?         */
  49.   {
  50.     pcx_encode(prev_data, data_count, fp);      /* Encode data  */
  51.   }
  52. }
  53.  
  54. /* Write an encoded byte pair (or single byte) to a file        */
  55.  
  56. void pcx_encode
  57. (
  58.   int data,     /* Data byte                                    */
  59.   int count,    /* Data byte repeat count                       */
  60.  
  61. @csource =   FILE *fp      /* PCX file pointer                             */
  62. )
  63. {
  64.   if (((data & 0xc0) == 0xc0) || count > 1)
  65.   {
  66.     putc(0xc0 | count, fp);     /* Write count byte             */
  67.   }
  68.  
  69.   putc(data, fp);       /* Write data byte                      */
  70. }
  71.